package com.zzptc.zzsgz.bdsjws.fragments;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.Add_Contacts_Activity;
import com.zzptc.zzsgz.bdsjws.adapter.Add_Contacts_Adapter;
import com.zzptc.zzsgz.bdsjws.fragments.base.BaseFragment;
import com.zzptc.zzsgz.bdsjws.vo.Contacts;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/27.
 */

public class SecondFragment extends BaseFragment implements View.OnClickListener {


    private EditText editText;
    private ImageView imageView;
    private TextView textView;
    private static RecyclerView recyclerview_sos;
    private static List<Contacts> list = new ArrayList<>();
    private static Add_Contacts_Adapter add_contacts_adapter;

    @Override
    public int getXML() {
        return R.layout.second_fragment_layout;
    }

    @Override
    public void initView() {
        editText = (EditText) getThisView().findViewById(R.id.edit_num_contacts);
        editText.addTextChangedListener(textWatcher);
        imageView = (ImageView) getThisView().findViewById(R.id.img_add_contacts);
        imageView.setOnClickListener(this);
        textView = (TextView) getThisView().findViewById(R.id.tv_add_contacts);
        textView.setOnClickListener(this);
        getThisView().findViewById(R.id.btn_yes_contacts).setOnClickListener(this);
        recyclerview_sos= (RecyclerView) getThisView().findViewById(R.id.recyclerview_sos);
        recyclerview_sos.setLayoutManager(new LinearLayoutManager(MyApplication.getContext()));
        add_contacts_adapter = new Add_Contacts_Adapter(list);
        recyclerview_sos.setAdapter(add_contacts_adapter);
    }

    @Override
    public void initDate() {

    }

    TextWatcher textWatcher=new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if(charSequence.length()>0){
                imageView.setVisibility(View.GONE);
                textView.setVisibility(View.VISIBLE);
            }else {
                textView.setVisibility(View.GONE);
                imageView.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_add_contacts:
                startActivity(new Intent(getActivity(), Add_Contacts_Activity.class));
                break;
            case R.id.tv_add_contacts:

                break;
            case R.id.btn_yes_contacts:

                break;
        }
    }

    public static void onNotifyDataSetChanged(){
        list.clear();
        list.addAll( MyApplication.getChecked_contacts_list() );
        recyclerview_sos.setVisibility(View.VISIBLE);
        add_contacts_adapter.notifyDataSetChanged();
    }

}
